package com.niuwa.p2p.action.channel;

import java.math.BigDecimal;
import java.util.Map;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;

import com.niuwa.p2p.core.mvc.entity.JsonResult;
import com.niuwa.p2p.core.mvc.entity.PageBeanWithTitle;
import com.niuwa.p2p.core.utils.DictConstant;
import com.niuwa.p2p.opr.utils.SessionUtil;
import com.niuwa.p2p.service.channel.IChannelCustomerService;
import com.niuwa.p2p.service.channel.IChannelInfoService;
import com.niuwa.p2p.vo.channel.AuditFrom;
import com.niuwa.p2p.vo.channel.ChannelCustomerTitleVo;
import com.niuwa.p2p.vo.channel.ChannelCustomerVo;

/*
 * 
 * @title ChannelInfoAction.java
 *
 * @author Cupid Wu
 * @version 1.0
 * @created 2015年4月7日
 */

@Namespace("/channel")
@Controller
public class ChannelInfoAction extends ChannelCommonAction{

    private static final long serialVersionUID = 1652877688835761694L;
    @Autowired
    private IChannelCustomerService channelCustomerService;
    @Autowired
    private IChannelInfoService channelInfoService;
    
    private String status;
	
	private String auditStatus;
	
	private AuditFrom auditFrom;
	
    private BigDecimal changeAmount;
    
    private String remark;
    
    private String channelCode;
    
    //逻辑删除Logo
    @Action("deleteChannelLogo")
    public String deleteImgVideo() {
        String message = "文件删除失败";
        channelCustomerService.deleteChannelLogo(channelCode);
        message = "你已成功删除文件";
        JsonResult result = new JsonResult();
        result.setMsg(message);
        this.setResult(result);
        return NONE;
    }
    
    @Action("addChannelBasicInfo")
    public String addChannelBasicInfo() throws Exception{
        validateChannelInfo();
        JsonResult result = new JsonResult();
        String channelCode;
        //填充账户类型
        this.getCbiv().getCorporation().setCorpAcctType(DictConstant.CORP_ACCOUNT_TYPE.GUARANTEE_CORP.getCode());
        if(StringUtils.isEmpty(this.getCbiv().getChannelInfo().getChannelCode())){
            channelCode = channelCustomerService.addNewChannelCustomer(this.getCbiv());
            result.setSuccess(true);
            result.setMsg("渠道创建成功");
            result.setData(channelCode);
            this.setResult(result);
        }else{
            channelCustomerService.updateChannelCustomer(this.getCbiv());
            result.setSuccess(true);
            result.setMsg("渠道更新成功");
            this.setResult(result);
        }
        return NONE;
    }
    /**
     * 獲得某個渠道的信息
     * @return
     * @throws Exception
     */
    @Action("getChannelBasicInfo")
    public String getChannelBasicInfo() throws Exception{
        JsonResult result = new JsonResult();
        ChannelCustomerVo channelCustomerVo=channelInfoService.getChannelBasicInfo(this.getChannelCode(),SessionUtil.getSessionUser());
        result.setSuccess(true);
        result.setData(channelCustomerVo);
        this.setResult(result);
        return NONE;
    }

    /**
	 *  渠道额度列表分页查询
	 * @return
	 */
 	@Action("listChannelLimit")
 	public String listChannelLimt() { 	
 	// 获取Grid条件选择参数
 		Map<String, Object> paramMap = getParamMap();
 		PageBeanWithTitle<ChannelCustomerTitleVo, ChannelCustomerVo> pagination = channelInfoService.searchIncludeTitle(paramMap);// 执行查询操作
 		setObjectReSult(pagination);
		return NONE;
 	}
    /**
	 * 恢复、禁用渠道状态
	 * @throws Exception 
	 */	
	@Action("updateChannelStatus")
	public String updateChannelStatus() throws Exception {

		JsonResult result = new JsonResult();		
		channelInfoService.updateChannelStatus(this.getChannelCode(), SessionUtil.getSessionUser().getId(),getStatus());		
		result.setSuccess(true);
		result.setMsg("操作成功");
		this.setResult(result);
		return NONE;
	}
	/**
	 * 调整渠道额度
	 * @return
	 */
	@Action("updateChannelApproveAmount")
	public String updateChannelApproveAmount(){
		JsonResult result = new JsonResult();		
		channelInfoService.updateChannelApproveAmount(this.getChannelCode(), SessionUtil.getSessionUser().getId(),remark,changeAmount);		
		result.setSuccess(true);
		result.setMsg("操作成功");
		this.setResult(result);
		return NONE;
	}
	public String getStatus() {
		return status;
	}

	public void setStatus(String status) {
		this.status = status;
	}

	public String getAuditStatus() {
		return auditStatus;
	}

	public void setAuditStatus(String auditStatus) {
		this.auditStatus = auditStatus;
	}

	public AuditFrom getAuditFrom() {
		return auditFrom;
	}

	public void setAuditFrom(AuditFrom auditFrom) {
		this.auditFrom = auditFrom;
	}


	public String getRemark() {
		return remark;
	}

	public void setRemark(String remark) {
		this.remark = remark;
	}
	
	public String getChannelCode(){
	    return channelCode;
	}
	
	public void setChannelCode(String channelCode){
	    this.channelCode = channelCode;
	}

	public BigDecimal getChangeAmount() {
		return changeAmount;
	}

	public void setChangeAmount(BigDecimal changeAmount) {
		this.changeAmount = changeAmount;
	}
	
}
